﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OBUActivation
{
    /// <summary>
    /// 读卡器发送命令封装类
    /// </summary>
    class ESAMCmd
    {
        /// <summary>
        /// DECREASE COUNTER
        /// </summary>
        /// <returns></returns>
        public static byte[] DecreaseCounter()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0x00);
            _list.Add(0x59);
            _list.Add(0x00);
            _list.Add(0x00);
            _list.Add(0x01);
            return _list.ToArray();
        }

        /// <summary>
        /// GET CHALLENGE
        /// </summary>
        /// <param name="le">随机数长度0X04、0X08</param>
        /// <returns></returns>
        public static byte[] GetChallenge(byte le)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0x00);
            _list.Add(0x84);
            _list.Add(0x00);
            _list.Add(0x00);
            _list.Add(le);
            return _list.ToArray();
        }

        /// <summary>
        /// GET RESPONSE
        /// </summary>
        /// <param name="le">响应的最大数据长度</param>
        /// <returns></returns>
        public static byte[] GetResponse(byte le)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0x00);
            _list.Add(0xC0);
            _list.Add(0x00);
            _list.Add(0x00);
            _list.Add(le);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ DATA 命令
        /// </summary>
        /// <param name="rand">八字节随机数</param>
        /// <param name="len">期望读取的明文长度</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] ReadData(ulong rand,byte len,byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0XB4);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X0A);
            _list.AddRange(SamUtil.ConvertLong2Array(rand));
            _list.Add(len);
            _list.Add(keyVersion);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ BINARY 命令
        /// </summary>
        /// <param name="p1">文件号，如 80 + SFI，...</param>
        /// <param name="p2">低地址，开始读取位置</param>
        /// <param name="le">期望读取到的数据长度</param>
        /// <returns>命令报文</returns>
        public static byte[] ReadBinary(byte p1, byte p2, byte le)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0XB0);
            _list.Add(p1);
            _list.Add(p2);
            _list.Add(le);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ RECORD 命令
        /// </summary>
        /// <param name="p1">记录号</param>
        /// <param name="p2">(SFI << 3) & 0XF8 + 4</param>
        /// <param name="le">期望读取到的记录长度</param>
        /// <returns>命令报文</returns>
        public static byte[] ReadRecord(byte p1, byte p2, byte le)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0XB2);
            _list.Add(p1);
            _list.Add(p2);
            _list.Add(le);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 SELECT FILE 命令
        /// </summary>
        /// <param name="file">文件名，如 DF03...</param>
        /// <returns>命令报文</returns>
        public static byte[] SelectFile(ushort file)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0XA4);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X02);
            _list.Add((byte)(file >> 8 & 0X00FF));
            _list.Add((byte)(file >> 0 & 0X00FF));
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE BINARY 命令
        /// </summary>
        /// <param name="p1">文件号，如 80 + SFI，...</param>
        /// <param name="p2">低地址，开始更新位置</param>
        /// <param name="lc">更新的数据 + MAC 的长度</param>
        /// <param name="data">更新的数据</param>
        /// <param name="mac">校验码</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateBinary(byte p1, byte p2, byte lc, byte[] data, byte[] mac)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X04);
            _list.Add(0XD6);
            _list.Add(p1);
            _list.Add(p2);
            _list.Add(lc);
            _list.AddRange(data);
            _list.AddRange(mac);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE BINARY 命令
        /// </summary>
        /// <param name="p1">文件号，如 80 + SFI，...</param>
        /// <param name="p2">低地址，开始更新位置</param>
        /// <param name="lc">更新的数据的长度</param>
        /// <param name="data">更新的数据</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateBinary(byte p1, byte p2, byte lc, byte[] data)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0XD6);
            _list.Add(p1);
            _list.Add(p2);
            _list.Add(lc);
            _list.AddRange(data);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE RECORD 命令
        /// </summary>
        /// <param name="p1">记录号</param>
        /// <param name="p2">(SFI << 3) & 0XF8 + n</param>
        /// <param name="lc">更新的记录 + MAC 的长度</param>
        /// <param name="data">更新的记录</param>
        /// <param name="mac">校验码</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateRecord(byte p1, byte p2, byte lc, byte[] data, byte[] mac)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X04);
            _list.Add(0XDC);
            _list.Add(p1);
            _list.Add(p2);
            _list.Add(lc);
            _list.AddRange(data);
            _list.AddRange(mac);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE RECORD 命令
        /// </summary>
        /// <param name="p1">记录号</param>
        /// <param name="p2">(SFI << 3) & 0XF8 + n</param>
        /// <param name="lc">更新的记录的长度</param>
        /// <param name="data">更新的记录</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateRecord(byte p1, byte p2, byte lc, byte[] data)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0XDC);
            _list.Add(p1);
            _list.Add(p2);
            _list.Add(lc);
            _list.AddRange(data);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE KEY 命令
        /// </summary>
        /// <param name="p2">P2 参数</param>
        /// <param name="lc">加密密钥 + MAC 的长度</param>
        /// <param name="encryptedKey">加密密钥</param>
        /// <param name="mac">校验码</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateKey(byte p2, byte lc, byte[] encryptedKey, byte[] mac)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X84);
            _list.Add(0XD4);
            _list.Add(0X01);
            _list.Add(p2);
            _list.Add(lc);
            _list.AddRange(encryptedKey);
            _list.AddRange(mac);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ DYNAMICINFO1 命令
        /// 应用范围为收费交易模式一和二，用于生成VST中所需要的动态数据
        /// 包括获取RandOBU,OBE拆卸状态及OBE类型
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] ReadDynamicInfo1()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XE0);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X09);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ DYNAMICINFO2 命令
        /// 应用范围为稽查交易模式，用于生成发送VST时所需要的动态数据
        /// 包括获得OBE拆卸状态及OBE类型
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] ReadDynamicInfo2()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XE2);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X01);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ OBEINFO1 命令
        /// 命令报文没有修改，响应报文有所修改，具体参考附录2-2
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] ReadObuInfo1()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XE4);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X1A);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ TIME 命令
        /// </summary>
        /// <param name="p1">
        ///     P1 = 00：表示获取专用命令6的最小最大时间
        ///     P1 = 01：表示获取专用命令7的最小最大时间
        /// </param>
        /// <returns>命令报文</returns>
        public static byte[] ReadTime(byte p1)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XE8);
            _list.Add(p1);
            _list.Add(0X00);
            _list.Add(0X02);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 ACCOMPLISH ACCESSCREDENTIALS1 命令
        /// 应用范围为收费交易模式一，用于完成对RSU的访问许可证accessCredentials
        /// 及RSU生成的随机数randRSUforAuthen生成信息鉴别码Authenticator（返回值）
        /// </summary>
        /// <param name="accessCredentials">访问凭证</param>
        /// <param name="randRSUforAuthen">认证随机数</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] AccomplishAccessCredentials1(byte[] accessCredentials, byte[] randRSUforAuthen, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XEA);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X11);
            _list.AddRange(accessCredentials);
            _list.AddRange(randRSUforAuthen);
            _list.Add(keyVersion);
            _list.Add(0X08);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 ACCOMPLISH ACCESSCREDENTIALS2 命令
        /// 验证RSU发送过来的访问许可证是否正确
        /// </summary>
        /// <param name="accessCredentials">访问凭证</param>
        /// <param name="randRSUforAuthen">认证随机数</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] AccomplishAccessCredentials2(byte[] accessCredentials, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XEC);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X09);
            _list.AddRange(accessCredentials);
            _list.Add(keyVersion);
            _list.Add(0X00);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 EXPAND UPDATE RECORD1 命令
        /// 应用范围为收费交易模式一和二，用于将收到的交易信息写入ESAM交易记录文件中
        /// 并将OBU的终端交易流水号和OBE电量信息及OBE MAC ID校验信息/TAC码也写入交易记录
        /// </summary>
        /// <param name="tradeData">交易信息帧</param>
        /// <returns>命令报文</returns>
        public static byte[] ExpandUpdateRecord1(byte[] tradeData)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XD2);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X28);
            _list.AddRange(tradeData);
            _list.Add(0X00);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 GET AUTHENTICATION 命令
        /// 应用范围为收费交易模式二及稽查交易模式（BST携带随机数时使用）
        /// 对BST中随机数进行加密，生成信息鉴别码Authenticator
        /// </summary>
        /// <param name="randRSUforAuthen">认证随机数</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] GetAuthentication(byte[] randRSUforAuthen, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XD6);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X09);
            _list.AddRange(randRSUforAuthen);
            _list.Add(keyVersion);
            _list.Add(0X08);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 GET TAC1 命令
        /// 应用范围为收费交易模式一和二，用于生成TAC码实现后台对OBU的离线认证
        /// </summary>
        /// <param name="keyVersion">密钥版本号</param>
        /// <param name="crc">循环校验码</param>
        /// <param name="psamID">PSAM 设备号</param>
        /// <returns>命令报文</returns>
        public static byte[] GetTac1(byte keyVersion, ushort crc, byte[] psamID)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XD8);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X09);
            _list.Add(keyVersion);
            _list.AddRange(SamUtil.ConvertShort2Array(crc));
            _list.AddRange(psamID);
            _list.Add(0X08);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 SET KEYINDEX 命令
        /// 应用范围为收费交易模式一和二及稽查交易模式
        /// 用于设置密钥版本号
        /// </summary>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] SetKeyIndex(byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XDA);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X01);
            _list.Add(keyVersion);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 EXPAND GetSN 命令
        /// 读取卡片唯一的序列号（合同序列号？）
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] ExpandGetSN()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XDC);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X0A);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 EXPAND UPDATE RECORD2 命令
        /// 应用范围为收费交易模式一和二，用于将收到的交易信息写入ESAM交易记录文件中
        /// 并将OBU的终端交易流水号和OBE电量信息及OBE MAC ID校验信息/TAC码也写入交易记录
        /// </summary>
        /// <param name="tradeData">交易信息帧</param>
        /// <returns>命令报文</returns>
        public static byte[] ExpandUpdateRecord2(byte[] tradeData)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XDE);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X23);
            _list.AddRange(tradeData);
            _list.Add(0X00);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 GET TAC2 命令
        /// 应用范围为停车场及封闭道路收费，用于生成入口TAC码实现后台对OBU的离线认证
        /// </summary>
        /// <param name="keyVersion">密钥版本号</param>
        /// <param name="data">用于计算 TAC 码数据</param>
        /// <returns>命令报文</returns>
        public static byte[] GetTac2(byte keyVersion, byte[] data)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XC0);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X0E);
            _list.Add(keyVersion);
            _list.AddRange(data);
            _list.Add(0X08);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 GET TAC3 命令
        /// 应用范围为停车场及封闭式道路收费，用于生成出口TAC码实现后台对OBU的离线认证
        /// </summary>
        /// <param name="keyVersion">密钥版本号</param>
        /// <param name="data">用于计算 TAC 码数据</param>
        /// <returns>命令报文</returns>
        public static byte[] GetTac3(byte keyVersion, byte[] data)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XC2);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X11);
            _list.Add(keyVersion);
            _list.AddRange(data);
            _list.Add(0X08);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE TOLL RECORD 命令
        /// </summary>
        /// <param name="mac">校验码</param>
        /// <param name="data">收费记录文件中的出口信息</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateTollRecord(byte[] mac, byte[] data)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XC4);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X1D);
            _list.AddRange(mac);
            _list.AddRange(data);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE CONTROLLIN RECORD 命令
        /// </summary>
        /// <param name="mac">校验码</param>
        /// <param name="data">收费控制文件</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateControllinRecord(byte[] mac, byte[] data, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XC6);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X1D);
            _list.AddRange(mac);
            _list.AddRange(data);
            _list.Add(keyVersion);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE TRANS AMOUNT 命令
        /// </summary>
        /// <param name="mac">校验码</param>
        /// <param name="data">收费控制文件</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateTransAmount(byte[] mac, ushort amount, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XC8);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X07);
            _list.AddRange(mac);
            _list.AddRange(SamUtil.ConvertShort2Array(amount));
            _list.Add(keyVersion);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 UPDATE ELECTRONIC WALLET 命令
        /// </summary>
        /// <param name="mac">校验码</param>
        /// <param name="data">交易金额</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] UpdateElectronicWallet(byte[] mac, ulong amount, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XCA);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X0D);
            _list.AddRange(mac);
            _list.AddRange(SamUtil.ConvertLong2Array(amount));
            _list.Add(keyVersion);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ ENTERINFO 命令
        /// 应用范围为停车场/告诉公路应用中车辆入口时读取其交易金额记录文件中的
        /// 当前余额和DF03目录下车辆信息文件中的车辆信息。包括：车牌号/车型/车辆用户类型/当前交易余额等
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] ReadEnterInfo()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XCC);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X10);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ EXITINFO 命令
        /// 应用范围为停车场/高速公路应用中车辆出口时读取其收费控制文件中的
        /// 入口信息和DF03目录下车辆信息文件中的车辆信息
        /// 包括：车牌号，车型，车辆用户类型，入口站点号，入口车道号，
        /// 入口时间，入口PSAMID，入口TAC码，入口状态等
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] ReadExitInfo()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0XD0);
            _list.Add(0XCE);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X26);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 Reset 命令
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] Reset()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X80);
            _list.Add(0X70);
            _list.Add(0X10);
            _list.Add(0X11);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 QueryStatus 命令
        /// 01代表武汉ESAM应用
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] QueryStatus()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X80);
            _list.Add(0X72);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X01);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 QueryChipStatus 命令
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] QueryChipStatus()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X80);
            _list.Add(0X74);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X04);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 Activate 命令
        /// 采用IC卡激活OBU时使用
        /// </summary>
        /// <param name="activeData">激活数据</param>
        /// <returns>命令报文</returns>
        public static byte[] Activate(byte[] activeData)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X80);
            _list.Add(0X76);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X19);
            _list.AddRange(activeData);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 READ SYSTEM INFO DATA 命令
        /// 读取系统信息文件
        /// </summary>
        /// <param name="rand">随机数</param>
        /// <param name="len">期望读到的明文长度</param>
        /// <param name="keyVersion">密钥版本号</param>
        /// <returns>命令报文</returns>
        public static byte[] ReadSystemInfoData(ulong rand, byte len, byte keyVersion)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X80);
            _list.Add(0X78);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X0A);
            _list.AddRange(SamUtil.ConvertLong2Array(rand));
            _list.Add(len);
            _list.Add(keyVersion);
            _list.Add(0X00);
            return _list.ToArray();
        }

        /// <summary>
        /// 测试 GET CIPHER DATA 命令
        /// </summary>
        /// <returns>命令报文</returns>
        public static byte[] GetCipherData()
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X80);
            _list.Add(0X7A);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X15);
            return _list.ToArray();
        }

        /// <summary>
        /// 解析 INTERNAL AUTHENTICATION 应答
        /// </summary>
        /// <param name="rand">随机数</param>
        /// <returns>命令报文</returns>
        public static byte[] InternalAuthentication(ulong rand)
        {
            List<byte> _list = new List<byte>();
            _list.Add(0X00);
            _list.Add(0X88);
            _list.Add(0X00);
            _list.Add(0X00);
            _list.Add(0X08);
            _list.AddRange(SamUtil.ConvertLong2Array(rand));
            _list.Add(0X10);
            return _list.ToArray();
        }

    }
}
